package com.framework.view.indicator;

import android.content.Context;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.framework.view.indicator.configs.LayoutConfig;
import com.framework.view.indicator.util.ArgbEvaluatorHolder;

import java.util.List;

/**
 * 自定义布局的标题
 *
 * @author liangzx
 * @version 1.0
 * @time 2020-04-21 17:43
 **/
class CustomLayoutTitleView extends FrameLayout implements IPageTitle<LayoutIndicatorAdapter>, View.OnClickListener {

    LayoutIndicatorAdapter mAdapter;
    /**
     * 配置信息
     */
    private LayoutConfig mConfig;
    /**
     * 位置
     */
    private int mPosition;
    /**
     * 是否需要监听滑动
     */
    private boolean mIsNeedListenSlide;
    /**
     * 子视图
     */
    private View mChildView;
    /**
     * 回调给父控件切换viewpager
     */
    private OnTitleClickListener mChildClickListener;

    public CustomLayoutTitleView(@NonNull Context context) {
        super(context);
    }

    @Override
    public void init(LayoutIndicatorAdapter adapter, int position) {
        mAdapter = adapter;
        mConfig = adapter.getTitleConfig();
        mPosition = position;
        setPadding(mConfig.getPendingLeft(), mConfig.getPendingTop(), mConfig.getPendingRight(), mConfig.getPendingBottom());
        mIsNeedListenSlide = mConfig.isColorGradientFollowSlide() || mConfig.isSizeFollowSlideChange();

        final int itemLayoutId = adapter.getItemLayoutId();
        mChildView = LayoutInflater.from(getContext()).inflate(itemLayoutId, this, false);
        adapter.bindItemTitleView(mChildView, position, adapter.getItem(position));
        LayoutParams layoutParams = (LayoutParams) mChildView.getLayoutParams();
        if (null == layoutParams) {
            layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        layoutParams.gravity = Gravity.CENTER;
        addView(mChildView, layoutParams);

        if (null != mConfig.getClickIds() && !mConfig.getClickIds().isEmpty()) {
            for (int id : mConfig.getClickIds()) {
                View viewById = mChildView.findViewById(id);
                if (null == viewById) {
                    continue;
                }
                viewById.setOnClickListener(this);
            }
        }

        setTxtColorAndSizeByChanged(position == 0);
    }

    @Override
    public void onEntering(int index, int totalCount, float enterPercent, boolean leftToRight) {
        setTxtColorAndSizeByChanging(enterPercent, true);
    }

    @Override
    public void onLeaving(int index, int totalCount, float leavePercent, boolean leftToRight) {
        setTxtColorAndSizeByChanging(leavePercent, false);
    }

    @Override
    public void onSelected(int index, int totalCount) {
        setTxtColorAndSizeByChanged(true);
    }

    @Override
    public void onCancelSelected(int index, int totalCount) {
        setTxtColorAndSizeByChanged(false);
    }

    /**
     * 改变所有文本控件的颜色或大小
     *
     * @param percent
     * @param isEntering
     */
    private void setTxtColorAndSizeByChanging(float percent, boolean isEntering) {
        if (mIsNeedListenSlide) {
            List<Integer> needAutoChangeTxtIds = mConfig.getNeedAutoChangeTxtIds();
            if (null == needAutoChangeTxtIds || needAutoChangeTxtIds.isEmpty()) {
                return;
            }
            final float sizePercent = percent * 2 >= 1 ? 1 : percent * 2;
            int i = 0;
            for (int id : needAutoChangeTxtIds) {
                View viewById = findViewById(id);
                if (viewById instanceof TextView) {
                    TextView textView = (TextView) viewById;
                    if (mConfig.isColorGradientFollowSlide()) {
                        final int color;
                        if (isEntering) {
                            color = ArgbEvaluatorHolder.eval(percent, mConfig.getNormalColorByPosition(i), mConfig.getSelectedColorByPosition(i));
                        } else {
                            color = ArgbEvaluatorHolder.eval(percent, mConfig.getSelectedColorByPosition(i), mConfig.getNormalColorByPosition(i));
                        }
                        if (0 != color) {
                            textView.setTextColor(color);
                        }
                    }
                    if (mConfig.isSizeFollowSlideChange()) {
                        final int normalSize = mConfig.getNormalTxtSizeByPosition(i);
                        final int selectedSize = mConfig.getSelectedTxtSizeByPosition(i);
                        final float size;
                        if (isEntering) {
                            size = mConfig.getNormalTxtSizeByPosition(i) + (selectedSize - normalSize) * sizePercent;
                        } else {
                            size = mConfig.getSelectedTxtSizeByPosition(i) - (selectedSize - normalSize) * sizePercent;
                        }
                        if (0 != size) {
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
                        }
                    }
                }
                i++;
            }
        }
    }

    /**
     * 改变所有文本控件的颜色或大小
     *
     * @param isSelected
     */
    private void setTxtColorAndSizeByChanged(boolean isSelected) {
        if (mConfig.isSizeFollowSlideChange() && mConfig.isColorGradientFollowSlide()) {
            setTxtBoldByChanged(isSelected);
            return;
        }
        List<Integer> needAutoChangeTxtIds = mConfig.getNeedAutoChangeTxtIds();
        if (null == needAutoChangeTxtIds || needAutoChangeTxtIds.isEmpty()) {
            return;
        }
        final boolean hasBoldSettings = mConfig.isNormalBold() || mConfig.isSelectedBold();
        int i = 0;
        for (int id : needAutoChangeTxtIds) {
            View viewById = findViewById(id);
            if (viewById instanceof TextView) {
                TextView textView = (TextView) viewById;
                if (!mConfig.isColorGradientFollowSlide()) {
                    final int color;
                    if (isSelected) {
                        color = mConfig.getSelectedColorByPosition(i);
                    } else {
                        color = mConfig.getNormalColorByPosition(i);
                    }
                    if (0 != color) {
                        textView.setTextColor(color);
                    }
                }
                if (!mConfig.isSizeFollowSlideChange()) {
                    final int size;
                    if (isSelected) {
                        size = mConfig.getSelectedTxtSizeByPosition(i);
                    } else {
                        size = mConfig.getNormalTxtSizeByPosition(i);
                    }
                    if (0 != size) {
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
                    }
                }
                if (hasBoldSettings) {
                    if ((isSelected && mConfig.isSelectedBold())
                            || (!isSelected && mConfig.isNormalBold())) {
                        textView.setTypeface(Typeface.DEFAULT_BOLD);
                    } else {
                        textView.setTypeface(Typeface.DEFAULT);
                    }
                }
            }
            i++;
        }
    }

    /**
     * 设置字体变粗
     *
     * @param isSelected
     */
    private void setTxtBoldByChanged(boolean isSelected) {
        List<Integer> needAutoChangeTxtIds = mConfig.getNeedAutoChangeTxtIds();
        if (null == needAutoChangeTxtIds || needAutoChangeTxtIds.isEmpty()) {
            return;
        }
        final boolean hasBoldSettings = mConfig.isNormalBold() || mConfig.isSelectedBold();
        if (hasBoldSettings) {
            for (int id : needAutoChangeTxtIds) {
                View viewById = findViewById(id);
                if (viewById instanceof TextView) {
                    TextView textView = (TextView) viewById;
                    if ((isSelected && mConfig.isSelectedBold())
                            || (!isSelected && mConfig.isNormalBold())) {
                        textView.setTypeface(Typeface.DEFAULT_BOLD);
                    } else {
                        textView.setTypeface(Typeface.DEFAULT);
                    }
                }
            }
        }
    }

    @Override
    public int getContentLeft() {
        return getLeft() + mChildView.getLeft();
    }

    @Override
    public int getContentRight() {
        return getContentLeft() + (mChildView.getRight() - mChildView.getLeft());
    }

    @Override
    public int getContentTop() {
        return mChildView.getTop();
    }

    @Override
    public int getContentBottom() {
        return mChildView.getBottom();
    }

    @Override
    public void onClick(View v) {
        if (null != mAdapter.getOnItemClickListener()) {
            mAdapter.getOnItemClickListener().onItemClick(mAdapter, mChildView, v, mPosition);
            if (null != mChildClickListener) {
                mChildClickListener.onTitleClick(mPosition);
            }
        }
    }

    /**
     * 用于回调给父控件切换页面
     *
     * @param childClickListener
     */
    void setOnChildClickListener(OnTitleClickListener childClickListener) {
        mChildClickListener = childClickListener;
    }
}
